package com.atguigu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/24 10:10
 */
public class Flink11_Hive {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
      
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
    
        tenv.executeSql("create table person(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int " +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv' " +
                            ")");
       
        
        // 1. 创建hive catalog
        HiveCatalog hive = new HiveCatalog("hive", "gmall", "input/");
    
        // 2. 注册hive catalog
        tenv.registerCatalog("hive", hive);
        tenv.useCatalog("hive");
        tenv.useDatabase("gmall");
        
        // 3. 使用hive catalog读取hive的数据
//        tenv.sqlQuery("select * from hive.gmall.person").execute().print();
        tenv.sqlQuery("select * from person").execute().print();
        
        tenv.useCatalog("default_catalog");
        tenv.useDatabase("default_database");
        tenv.sqlQuery("select * from person").execute().print();
    }
}
